package com.vincent.seeyou.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.vincent.seeyou.R;
import com.vincent.seeyou.SYApplication;
import com.vincent.seeyou.adapter.UserFriendAdapter;
import com.vincent.seeyou.model.User;
import com.vincent.seeyou.ui.PersonInfoActivity;
import com.vincent.seeyou.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cn.bmob.im.bean.BmobChatUser;
import cn.bmob.im.util.BmobLog;

/**
 * Created by vincent on 14-8-28.
 */
public class ContactsFragment extends BaseFragment implements AdapterView.OnItemClickListener{

    private ListView listView_friends;
    private UserFriendAdapter userAdapter;
    List<User> friends = new ArrayList<User>();
    Map <String, BmobChatUser > users;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_contacts, container, false);


        listView_friends = (ListView)rootView.findViewById(R.id.listView_friends);
        listView_friends.setOnItemClickListener(this);
        userAdapter = new UserFriendAdapter(getActivity(),friends);
        listView_friends.setAdapter(userAdapter);
        return rootView;
    }


    @Override
    public void onResume() {
        super.onResume();
        if(!hidden){
            refresh();
        }
    }


    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
        User user = (User) userAdapter.getItem(position);

        BmobLog.i(user.getUsername()+"***********************");
        //先进入好友的详细资料页面
        Intent intent =new Intent(getActivity(),PersonInfoActivity.class);
        intent.putExtra("userName", user.getUsername());
        startAnimActivity(intent);
    }

    private boolean hidden;
    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        this.hidden = hidden;
        if(!hidden){
            refresh();
        }
    }

    public void refresh(){
        try {
            getActivity().runOnUiThread(new Runnable() {
                public void run() {
                    queryMyfriends();
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private void queryMyfriends() {

        Map<String,BmobChatUser> users = SYApplication.getInstance().getContactList();
        //组装新的User
        filledData(CollectionUtils.map2list(users));
        if(userAdapter==null){
            userAdapter = new UserFriendAdapter(getActivity(), friends);
            listView_friends.setAdapter(userAdapter);
        }else{
            userAdapter.notifyDataSetChanged();
        }

    }


    public  void filledData(List<BmobChatUser> bmobChatUsers) {

        friends.clear();
        int total = bmobChatUsers.size();
        for (int i = 0; i < total; i++) {
            BmobChatUser user = bmobChatUsers.get(i);
            User sortModel = new User();
            sortModel.setAvatar(user.getAvatar());
            sortModel.setNick(user.getNick());
            sortModel.setUsername(user.getUsername());
            sortModel.setObjectId(user.getObjectId());
            sortModel.setContacts(user.getContacts());

            friends.add(sortModel);
        }

    }

}

